import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class MapLoader {
	
	private Scanner input;
	String fileName  = "";
	
	public void setFileName(String fileName){
		this.fileName = fileName; 
	}
	
	
	public void openFile() {
		try {
			input = new Scanner( new File( fileName ) );
		}//end try
		catch ( FileNotFoundException fileNotFoundException ){
			System.err.println( "Error opening file");
			System.exit(1);
		}//end catch
	}//end openFile();
	
	public Map readMap(){
		Map map = new Map();
		Tile tile = new Tile();
		int inputRow = 0;
		int inputCol = 0;
		int directionInput  = 1;
		
		int exitTotal = 0;
		
		try{
				map.setMapName(input.nextLine());
				map.setActionListFileName(input.nextLine());
				map.setTileBase(input.nextInt());
				map.setColLength(input.nextInt());
				map.setRowLength(input.nextInt());
				map.setHeroStartRow(input.nextInt());
				map.setHeroStartCol(input.nextInt());
				map.setBgImageName(input.next());
				
				exitTotal = input.nextInt();
				Exit exit = new Exit();
				if(exitTotal > 0){
					for(int i = exitTotal;i>0;i--){
						exit.setExitRow( input.nextInt() );
						exit.setExitCol( input.nextInt() );
						exit.setExitFileName(input.next());
						exit.setPlayerDisembarkRow(input.nextInt());
						exit.setPlayerDisembarkCol(input.nextInt());
						
						map.addExit(exit);
						exit = new Exit();
					}
				}
							
							
				inputRow = input.nextInt();
				inputCol = input.nextInt();
				
				for(int row = 0; row < map.getRowLength();row++) {
					for(int col = 0; col < map.getColLength();col++){
																	
						if(inputRow == row && inputCol == col){ //if tiles match
							
								tile.setRow(inputRow);
								tile.setCol(inputCol);
								
								directionInput = input.nextInt();
								if(directionInput == 0)
									tile.setNorth(false);
								else
									tile.setNorth(true);
								
								directionInput = input.nextInt();
								if(directionInput == 0)
									tile.setSouth(false);
								else
									tile.setSouth(true);
								
								directionInput = input.nextInt();
								if(directionInput == 0)
									tile.setEast(false);
								else
									tile.setEast(true);
								
								directionInput = input.nextInt();
								if(directionInput == 0)
									tile.setWest(false);
								else
									tile.setWest(true);
																						
									//Check to make sure there is more data to read and input if there is
									if(row + 1 != map.getRowLength() ||
									   col + 1 != map.getColLength() ){
									inputRow = input.nextInt();
									inputCol = input.nextInt();}
							
						} else {//end if tiles do not match
							
							tile.setRow(row);
							tile.setCol(col);
							tile.setNorth(true);
							tile.setSouth(true);
							tile.setEast(true);
							tile.setWest(true);
						}//end else tiles dont match
						
											
						map.addTile(tile);
						//System.out.println(tile.report());
						tile = new Tile();
					}//end col for loop
				}//end row for loop
			
		}//end try
		catch(NoSuchElementException elementException){
			System.err.println("Wrong Form");
			System.exit(1);
		}
		catch(IllegalStateException stateException){
			System.err.println("Wront State");
			System.exit(1);
		}
				
		return map;
	}//readMap()

	public void closeFile()
	{
		if ( input != null )
				input.close();
		
	}
}


